package shoppingmall.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shoppingmall.manage.mapper.UserMapper;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.pojo.User;
import shoppingmall.manage.service.UserService;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询平台会员用户数据列表信息
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean getUserList(int page, int pageSize) {
        //获取查询起始索引
        int start = ( page - 1 ) * pageSize;
        List<User> userList = userMapper.getUserList(start, pageSize);
        //查询数据总条数
        int userCount = userMapper.getUserCount();
        //返回结果
        return new PageBean(userCount, userList);
    }

    /**
     * 修改一个平台会员用户信息
     * @param user
     * @return
     */
    @Override
    public boolean updateUser(User user) {
        try {
            userMapper.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除一个平台会员用户
     * @param userId
     * @return
     */
    @Override
    public boolean deleteUser(int userId) {
        try {
            userMapper.deleteUser(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除多个平台会员用户
     * @param ids
     * @return
     */
    @Override
    public boolean deleteUsers(List ids) {
        try {
            userMapper.deleteUsers(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 根据平台会员用户名模糊查询平台会员列表信息
     * @param username
     * @return
     */
    @Override
    public PageBean getUserListByLikeUsername(String username) {
        List<User> userList = userMapper.getUserListByLikeUsername(username);
        //返回结果
        return new PageBean(userList.size(), userList);
    }

    /**
     * 根据某一段时间内查询注册的平台会员信息(数据大屏)
     */
    @Override
    public PageBean getUserListByTimeQuantum(String startDate, String endDate) {
        List<User> userList = userMapper.getUserListByTimeQuantum(startDate, endDate);
        return new PageBean(userList.size(), userList);
    }

    /**
     * 查询平台注册会员总人数信息(数据大屏)
     */
    @Override
    public int getUserCount() {
        int userCount = userMapper.getUserCount();
        return userCount;
    }

    /**
     * 添加一个平台会员用户-前台api接口应该实现的，此处测试用
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUser(User user) {
        try {
            userMapper.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
